// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CameraCalibrationInfo.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Microsoft.Robotics.Vision.Runtime.GroundTruthExtraction
{
    using System;
    using System.IO;
    using System.Runtime.CompilerServices;
    using System.Runtime.Serialization;

    using Microsoft.Robotics.IO;
    using Microsoft.Robotics.Numerics;

    /// <summary>
    /// Class which encapsulates landmark information
    /// </summary>
    [DataContract]
    public class CameraCalibrationInfo
    {
        /// <summary>
        /// Camera calibration info filename
        /// </summary>
        public const string Filename = "WebcamCalibration.xml";

        /// <summary>
        ///  Focal Length in x-direction (FX) of the camera in pixels.
        /// </summary>
        [DataMember(Name = "ColorIntrinsicParamsFx")]
        private double colorIntrinsicParamsFx;

        /// <summary>
        /// Skew of the camera.
        /// </summary>
        [DataMember(Name = "ColorIntrinsicParamsSkew")]
        private double colorIntrinsicParamsSkew;

        /// <summary>
        ///  Focal Length in y-direction (FY) of the camera in pixels.
        /// </summary>
        [DataMember(Name = "ColorIntrinsicParamsFy")]
        private double colorIntrinsicParamsFy;

        /// <summary>
        /// Position of the principle x-axis of the camera in pixels.
        /// </summary>
        [DataMember(Name = "ColorIntrinsicParamsPx")]
        private double colorIntrinsicParamsPx;

        /// <summary>
        /// Position of the principle y-axis of the camera in pixels.
        /// </summary>
        [DataMember(Name = "ColorIntrinsicParamsPy")]
        private double colorIntrinsicParamsPy;

        /// <summary>
        /// Color Radial and Tangential Distortion Parameter k0 for camera.
        /// </summary>
        [DataMember(Name = "ColorIntrinsicParamsk0")]
        private double colorRDistortionParamsk0;

        /// <summary>
        /// Color Radial and Tangential Distortion Parameter k1 for camera.
        /// </summary>
        [DataMember(Name = "ColorIntrinsicParamsk1")]
        private double colorRDistortionParamsk1;

        /// <summary>
        /// Color Radial and Tangential Distortion Parameter k2 for camera.
        /// </summary>
        [DataMember(Name = "ColorIntrinsicParamsk2")]
        private double colorRDistortionParamsk2;

        /// <summary>
        /// Color Radial and Tangential Distortion Parameter k3 for camera.
        /// </summary>
        [DataMember(Name = "ColorIntrinsicParamsk3")]
        private double colorRDistortionParamsk3;

        /// <summary>
        /// Color Radial and Tangential Distortion Parameter k4 for camera.
        /// </summary>
        [DataMember(Name = "ColorIntrinsicParamsk4")]
        private double colorRDistortionParamsk4;

        /// <summary>
        /// Color Intrinsic Parameters for camera. 
        /// 0 - This is the Focal Length in x-direction (FX) of the camera in pixels.
        /// 1 - This is the Skew of the camera.
        /// 2 - This is the Focal Length in y-direction (FY) of the camera in pixels.
        /// 3 - This is the position of the principle x-axis of the camera in pixels.
        /// 4 - This is the position of the principle y-axis of the camera in pixels.
        /// For more information refer to HTTP://WWW.VISION.CALTECH.EDU/BOUGUETJ/CALIB_DOC/HTMLS/PARAMETERS.HTML
        /// </summary>     
        private double[] colorIntrinsicParams;

        /// <summary>
        /// Color Radial and Tangential Distortion Parameters for camera. (k0 - k4)
        /// For more information refer to HTTP://WWW.VISION.CALTECH.EDU/BOUGUETJ/CALIB_DOC/HTMLS/PARAMETERS.HTML
        /// </summary>        
        private double[] colorRDistortionParams;

        /// <summary>
        /// Gets or sets the ColorIntrinsicParams
        /// </summary>
        public double[] ColorIntrinsicParams
        {
            get
            {
                if (this.colorIntrinsicParams == null)
                {
                    this.colorIntrinsicParams = new double[5];
                }

                this.colorIntrinsicParams[0] = this.colorIntrinsicParamsFx;
                this.colorIntrinsicParams[1] = this.colorIntrinsicParamsSkew;
                this.colorIntrinsicParams[2] = this.colorIntrinsicParamsFy;
                this.colorIntrinsicParams[3] = this.colorIntrinsicParamsPx;
                this.colorIntrinsicParams[4] = this.colorIntrinsicParamsPy;
                return this.colorIntrinsicParams;
            }

            set
            {
                if (this.colorIntrinsicParams == null)
                {
                    this.colorIntrinsicParams = new double[5];
                }

                Array.Copy(value, this.colorIntrinsicParams, value.Length);
                this.colorIntrinsicParamsFx = this.colorIntrinsicParams[0];
                this.colorIntrinsicParamsSkew = this.colorIntrinsicParams[1];
                this.colorIntrinsicParamsFy = this.colorIntrinsicParams[2];
                this.colorIntrinsicParamsPx = this.colorIntrinsicParams[3];
                this.colorIntrinsicParamsPy = this.colorIntrinsicParams[4];
            }
        }

        /// <summary>
        /// Gets or sets the ColorRDistortionParams
        /// </summary>
        public double[] ColorRDistortionParams
        {
            get
            {
                if (this.colorRDistortionParams == null)
                {
                    this.colorRDistortionParams = new double[5];
                }

                this.colorRDistortionParams[0] = this.colorRDistortionParamsk0;
                this.colorRDistortionParams[1] = this.colorRDistortionParamsk1;
                this.colorRDistortionParams[2] = this.colorRDistortionParamsk2;
                this.colorRDistortionParams[3] = this.colorRDistortionParamsk3;
                this.colorRDistortionParams[4] = this.colorRDistortionParamsk4;
                return this.colorRDistortionParams;
            }

            set
            {
                if (this.colorRDistortionParams == null)
                {
                    this.colorRDistortionParams = new double[5];
                }

                Array.Copy(value, this.colorRDistortionParams, value.Length);
                this.colorRDistortionParamsk0 = this.colorRDistortionParams[0];
                this.colorRDistortionParamsk1 = this.colorRDistortionParams[1];
                this.colorRDistortionParamsk2 = this.colorRDistortionParams[2];
                this.colorRDistortionParamsk3 = this.colorRDistortionParams[3];
                this.colorRDistortionParamsk4 = this.colorRDistortionParams[4];
            }
        }
    }
}
